package com.neusoft.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.work.dao.BedAssignmentDao;
import com.neusoft.work.dao.BedInfoDao;
import com.neusoft.work.po.BedAssignment;
import com.neusoft.work.po.BedInfo;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.BedInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BedInfoServiceImpl implements BedInfoService {
    @Autowired
    private BedInfoDao bedInfoDao;

    @Autowired
    private BedAssignmentDao bedAssignmentDao;

    @Override
    public ResponseBean<Integer> updateStatusByBedNumber(String bedNumber, Integer status) {
        // 更新单个床位状态，使用 UpdateWrapper
        UpdateWrapper<BedInfo> uw = new UpdateWrapper<>();
        uw.eq("bedNumber", bedNumber)
          .set("status", status);
        int affected = bedInfoDao.update(null, uw);
        if (affected > 0) {
            return new ResponseBean<>(affected);
        } else {
            return new ResponseBean<>(500, "更新床位状态失败");
        }
    }

    @Override
    public ResponseBean<Integer> updateStatusByCustomer(Integer customerId, Integer status) {
        // 1. 查出客户当前有效的床位分配
        BedAssignment assign = bedAssignmentDao.selectOne(
            new QueryWrapper<BedAssignment>()
                .eq("customerId", customerId)
                .eq("isDeleted", false)
                .last("LIMIT 1")
        );
        if (assign == null) {
            return new ResponseBean<>(500, "未找到客户当前分配床位");
        }

        // 2. 按 bed_number 更新床位状态
        int affected = bedInfoDao.update(
            null,
            new UpdateWrapper<BedInfo>()
                .eq("bedNumber", assign.getBedNumber())
                .set("status", status)
        );

        return affected > 0
            ? new ResponseBean<>(affected)
            : new ResponseBean<>(500, "更新床位状态失败");
    }

    @Override
    public ResponseBean<List<String>> queryVacantBedNumber(String roomNumber) {
        List<String> list = bedInfoDao.getBedNumberByRoom(roomNumber);
        if (list != null && !list.isEmpty()) {
            return new ResponseBean<>(list);
        } else {
            return new ResponseBean<>(500, "无空闲床位");
        }
    }

    @Override
    public ResponseBean<Integer> queryEveryStatusAmount(Integer status) {
        // selectCount 返回 Long
        Long count = bedInfoDao.selectCount(
            new QueryWrapper<BedInfo>().eq("status", status)
        );
        return new ResponseBean<>(count.intValue());
    }

    @Override
    public ResponseBean<Integer> queryBedAmountByRoomNumber(String roomNumber) {
    	return null;
    }

    @Override
    public ResponseBean<List<String>> queryBedNumberByRoomNumber(String roomNumber) {
    	return null;
    }

    @Override
    public ResponseBean<Integer> queryStatusByBedNumber(String bedNumber) {
        BedInfo bi = bedInfoDao.selectOne(
            new QueryWrapper<BedInfo>().eq("bedNumber", bedNumber)
        );
        if (bi != null) {
            return new ResponseBean<>(bi.getStatus());
        } else {
            return new ResponseBean<>(500, "未找到床位信息");
        }
    }
    
    @Override
    public ResponseBean<List<BedInfo>> queryBedsByRoomNumber(String roomNumber) {
        try {
            List<BedInfo> beds = bedInfoDao.selectBedsByRoomNumber(roomNumber);
            if (beds != null && !beds.isEmpty()) {
                return new ResponseBean<>(beds);
            } else {
                return new ResponseBean<>(500, "该房间无床位信息");
            }
        } catch (Exception e) {
            return new ResponseBean<>(500, "查询失败: " + e.getMessage());
        }
    }

}
